package com.vae.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vae.domain.entity.MediaFiles;
import com.vae.domain.entity.MediaProcessHistory;
import com.vae.media.mapper.MediaFilesMapper;
import com.vae.media.mapper.MediaProcessHistoryMapper;
import com.vae.media.mapper.MediaProcessMapper;
import com.vae.media.service.MediaProcessService;
import com.vae.domain.entity.MediaProcess;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;

/**
 * (MediaProcess)表服务实现类
 *
 * @author makejava
 * @since 2024-04-15 22:18:38
 */
@Service
public class MediaProcessServiceImpl extends ServiceImpl<MediaProcessMapper, MediaProcess> implements MediaProcessService {
    @Autowired
    MediaFilesMapper mediaFilesMapper;

    @Autowired
    MediaProcessMapper mediaProcessMapper;

    @Autowired
    MediaProcessHistoryMapper mediaProcessHistoryMapper;

    /**
     * 获取待处理任务
     * @param shardIndex 分片序号
     * @param shardTotal 分片总数
     * @param count 获取记录数
     * @return
     */
    @Override
    public List<MediaProcess> getMediaProcessList(int shardIndex, int shardTotal, int count) {
        List<MediaProcess> mediaProcesses = mediaProcessMapper.selectListByShardIndex(shardTotal, shardIndex, count);
        return mediaProcesses;
    }

    /**
     * 开启一个任务
     * @param id 任务id
     * @return
     */
    @Override
    public boolean startTask(long id) {
        int result = mediaProcessMapper.startTask(id);
        return result<=0?false:true;
    }

    /**
     * 保存任务结果
     * @param taskId  任务id
     * @param status 任务状态
     * @param fileId  文件id
     * @param url url
     * @param errorMsg 错误信息
     */
    @Transactional
    @Override
    public void saveProcessFinishStatus(Long taskId, String status, String fileId, String url, String errorMsg) {
        //查出任务，如果不存在则直接返回
        MediaProcess mediaProcess = mediaProcessMapper.selectById(taskId);
        if (mediaProcess == null) {
            return;
        }
        //处理失败，更新任务处理结果
        LambdaQueryWrapper<MediaProcess> queryWrapperById = new LambdaQueryWrapper<MediaProcess>().eq(MediaProcess::getId, taskId);
        //处理失败
        if (status.equals("3")) {
            MediaProcess mediaProcess_u = new MediaProcess();
            mediaProcess_u.setStatus("3");
            mediaProcess_u.setErrormsg(errorMsg);
            mediaProcess_u.setFailCount(mediaProcess.getFailCount() + 1);
            mediaProcessMapper.update(mediaProcess_u, queryWrapperById);
            return;
        }
        //任务处理成功
        MediaFiles mediaFiles = mediaFilesMapper.selectById(fileId);
        if (mediaFiles != null) {
            if (mediaFiles != null) {
                //更新媒资文件中的访问url
                mediaFiles.setUrl(url);
                String regex = "\\..*$";
                String replacement = ".mp4";
                String mp4Filename = mediaFiles.getFilename().replaceAll(regex, replacement);
                //更新媒资文件中的文件名后缀
                mediaFiles.setFilename(mp4Filename);
                mediaFilesMapper.updateById(mediaFiles);
            }
            //处理成功，更新url和状态
            mediaProcess.setUrl(url);
            mediaProcess.setStatus("2");
            mediaProcess.setFinishDate(new Date());
            mediaProcessMapper.updateById(mediaProcess);

            //添加到历史记录
            MediaProcessHistory mediaProcessHistory = new MediaProcessHistory();
            BeanUtils.copyProperties(mediaProcess, mediaProcessHistory);
            //更新文件名后缀
            String regex = "\\..*$";
            String replacement = ".mp4";
            String mp4Filename = mediaProcessHistory.getFilename().replaceAll(regex, replacement);
            mediaProcessHistory.setFilename(mp4Filename);
            mediaProcessHistoryMapper.insert(mediaProcessHistory);
            //删除mediaProcess
            mediaProcessMapper.deleteById(mediaProcess.getId());

        }
    }
}